package com.wanmait.translate.system.controller;

import com.wanmait.translate.pojo.*;
import com.wanmait.translate.utils.CommonMessage;
import com.wanmait.translate.utils.DictMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/*
 * @Author huweibin
 * @Description 处理需要返回modelAndView
 * @Date 14:37 2020/11/7
 **/
@Controller
@RequestMapping("/system/")
public class SystemHandleController extends SystemParentController{


    private static final Logger LOGGER= LoggerFactory.getLogger(SystemHandleController.class);

    /*
     * @Author huweibin
     * @Description 根据类型决定回显内容（直接新增add：无操作/新增至父部门下parentAdd：先查父部门/编辑edit：根据id查本部门数据）
     * @Date 14:48 2020/11/7
     **/
    @RequestMapping("/department/departmentInput")
    public String departmentInput(String type, Integer id, Model model) {
        SystemDept systemDept = null;
        if (type == null||id==null) {
            return "/error/500";
        }
        switch (type) {
            case "edit": {
                //获得当前部门信息
                systemDept = systemDeptService.getSelfAndParentByPrimaryKey(id);
                model.addAttribute("edit", systemDept);
                break;
            }
            case "parentAdd": {
                //获得父部门信息
                systemDept = systemDeptService.getByPrimaryKey(id);
                model.addAttribute("parentAdd", systemDept);
            }
            case "add":{
                systemDept = systemDeptService.getRootSystemDept();
                model.addAttribute("add", systemDept);
            }
        }
        LOGGER.info("回显部门信息"+systemDept);
        return "system/department/departmentInput";
    }
    /*
     * @Author huweibin
     * @Description 根据类型决定回显内容（
     * 直接新增add：无操作
     * 新增至父岗位下parentAdd：先查父岗位
     * 编辑edit：根据id查本部门数据
     * ）
     * @Date 14:48 2020/11/7
     **/
    @RequestMapping("/post/postInput")
    public String postInput(String type, Integer id, Model model, HttpSession session) {
        SystemUser user = (SystemUser) session.getAttribute(CommonMessage.USER);
        SystemPost systemPost = null;
        if (type == null||id==null) {
            return "/error/500";
        }
        switch (type) {
            case "edit": {
                //获得当前岗位信息
                systemPost = systemPostService.getSelfAndParentByPrimaryKey(id);
                model.addAttribute("edit", systemPost);
                break;
            }
            case "parentAdd": {
                //获得父岗位信息
                systemPost = systemPostService.getByPrimaryKey(id);
                model.addAttribute("parentAdd", systemPost);
                model.addAttribute("user", user);
            }
            case "add":{
                systemPost = new SystemPost();
                systemPost.setName(CommonMessage.ROOT);
                systemPost.setId(0);
                model.addAttribute("add", systemPost);
                model.addAttribute("user", user);
            }
        }
        LOGGER.info(type+"回显岗位信息"+systemPost);
        return "system/post/postInput";
    }
    /*
     * @Author huweibin
     * @Description 根据父菜单根据类型决定回显内容
     * @Date 23:02 2020/11/14
     **/
    @RequestMapping("/menu/menuInput")
    public String menuInput(String type, Integer id, Model model, HttpSession session) {

        SystemMenu systemMenu = null;
        if (type == null||id==null) {
            return "/error/500";
        }
        List<SystemDict> byDictTypeKey = systemDictService.getByDictTypeKey(DictMessage.DICT_URL_TYPE);
        model.addAttribute("urlList", byDictTypeKey);
        switch (type) {
            case "edit": {
                //获得当前岗位信息
                systemMenu = systemMenuService.getSelfAndParentByPrimaryKey(id);
                model.addAttribute("edit", systemMenu);
                break;
            }
            case "parentAdd": {
                //获得父岗位信息
                systemMenu = systemMenuService.getByPrimaryKey(id);
                model.addAttribute("parentAdd", systemMenu);
            }
            case "add":{
                systemMenu = new SystemMenu();
                systemMenu.setName(CommonMessage.ROOT);
                systemMenu.setId(0);
                model.addAttribute("add", systemMenu);
            }
        }
        LOGGER.info(type+"回显菜单信息"+systemMenu);
        return "system/menu/menuInput";
    }
}
